<template>
  <a-modal :open="open" width="640px" title="转诊" centered :ok-button-props="{ disabled: !isCompleted }" ok-text="保存" @ok="onOk" @cancel="onCancel">
    <a-form :model="formState" class="form-style-flex">
      <a-form-item label=" " class="w-full">
        <a-radio-group v-model:value="formState.status">
          <a-radio :value="0">本院</a-radio>
          <a-radio disabled :value="1">外院</a-radio>
        </a-radio-group>
      </a-form-item>

      <a-form-item label="转诊医生" v-bind="validateInfos.empName" class="w-4/5">
        <!-- :search-params="{ksdm:formState.value.ksdm }" -->
        <SelectTable v-model:value="formState.empName" class="w-full" :search-params="{ jzbh: formState.jzbh }" type="转诊医生" @select="onSelectDoctor" />
      </a-form-item>
      <div class="w-1/5 pt-1 pl-5">{{ formState.ksmc }}</div>
      <a-form-item label="备注" class="w-full">
        <a-textarea v-model:value="formState.memo" :rows="5" placeholder="请输入" />
      </a-form-item>
      <div v-if="prompt" class="absolute text-red-500 bottom-6 left-12">{{ prompt }}</div>
    </a-form>
  </a-modal>
</template>
<script setup>
import { ref, watch } from 'vue'
import { message } from 'ant-design-vue'
import { post } from '@/utils/request'

const emit = defineEmits(['update:open', 'success'])
const props = defineProps({
  open: Boolean,
  rowData: {
    type: Object,
    default: () => ({})
  }
})
const formState = ref({
  empName: '',
  status: 0
})
const prompt = ref('')
const rulesState = ref({
  empName: [{ required: true, message: '请选择转诊医生' }]
})
const { isCompleted, resetFields, validate, validateInfos } = useForm(formState, rulesState)
const onSelectDoctor = (select = {}) => {
  prompt.value = select.prompt || ''
  formState.value.empId = select.empId
  formState.value.empName = select.empName
  formState.value.ksmc = select.ksmc
}

const onCancel = () => {
  emit('update:open', false)
  resetFields()
}
const onOk = async () => {
  await validate()
  const { code, msg } = await post('/outp/mzys/savePatientReferral', {
    brId: formState.value.brId,
    jzbh: props.rowData.jzbh,
    targetEmpId: formState.value.empId,
    memo: formState.value.memo,
    qmQrId: props.rowData.qmQrId
  })
  if (code === 0) {
    message.success(msg)
    emit('success')
    onCancel()
  } else {
    message.error(msg)
  }
}

watch(
  () => props.open,
  (val) => {
    if (val) {
      formState.value.ksdm = props.rowData.ksdm
      formState.value.jzbh = props.rowData.jzbh
    }
  }
)
</script>
